package pattern.tpt;

import lombok.extern.slf4j.Slf4j;

/**
 * 两阶段终止模式的volatile改进
 */
@Slf4j(topic = "c.TwoPhaseTerminationTest")
public class TwoPhaseTerminationTest2 {
    public static void main(String[] args) throws InterruptedException {
        TwoPhaseTermination tpt = new TwoPhaseTermination();
        tpt.start();
        tpt.start();
        tpt.start();

        /*Thread.sleep(3500);
        log.debug("停止监控");
        tpt.stop();*/
    }

    static class TwoPhaseTermination {
        // 监控线程
        private Thread monitorThread;
        // 停止标记
        private volatile boolean stop = false;
        // 判断是否执行过 start 方法
        private boolean starting = false;

        // 启动监控线程
        public void start() {
            // balking（犹豫模式）
            synchronized (this) {
                if (starting) { // false
                    return;
                }
                starting = true;
            }

            monitorThread = new Thread(() -> {
                while (true) {
                    Thread current = Thread.currentThread();
                    // 是否被打断
                    if (stop) {
                        log.debug("料理后事");
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                        log.debug("执行监控记录");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "monitor");
            monitorThread.start();
        }

        // 停止监控线程
        public void stop() {
            stop = true;
            monitorThread.interrupt();
        }
    }
}


